//
//  LocationButtonItem.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/27.
//

import UIKit
import RxSwift

class LocationButtonItem: UIBarButtonItem {
  private let label = UILabel()
  private var maxLength: Int = 10
  private let arraw = UIImageView(image: R.image.m_down_arraw())

  var tapClick: (() -> Void)?

  var text: String = "" {
    didSet {
      if text.count < maxLength {
        label.text = text
      } else {
        let length = text.index(text.startIndex, offsetBy: maxLength)
        label.text = "\(String(text[..<length]))..."
      }
    }
  }

  convenience init(maxLength: Int) {
    let stack = UIStackView(frame: .zero)
    stack.axis = .horizontal
    stack.spacing = 4
    self.init(customView: stack)

    stack.addArrangedSubview(label)
    stack.addArrangedSubview(arraw)
    self.maxLength = maxLength

    self.label.font = .designKit.title2

    let tap = UITapGestureRecognizer(target: self, action: #selector(tap))
    stack.addGestureRecognizer(tap)
  }


  @objc func tap() {
    tapClick?()
  }
}
